<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.SubstoreDAO" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Purreq" %>
<%@page import="db.OutreqDAO"%>
<%@page import="db.RetreqDAO"%>
<%@page import="db.BorreqDAO"%>
<%@page import="db.ToreqDAO"%>
<%@page import="db.BackreqDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	PurreqDAO purreqDAO = (PurreqDAO)ctx.getBean("PurreqDAO");
	OutreqDAO outreqDAO = (OutreqDAO)ctx.getBean("OutreqDAO");
	RetreqDAO retreqDAO = (RetreqDAO)ctx.getBean("RetreqDAO");
	BorreqDAO borreqDAO = (BorreqDAO)ctx.getBean("BorreqDAO");
	ToreqDAO toreqDAO = (ToreqDAO)ctx.getBean("ToreqDAO");
	BackreqDAO backreqDAO = (BackreqDAO)ctx.getBean("BackreqDAO");
	
	int pn=0;
	String pageNo = request.getParameter("pageNo");
	if(pageNo==null || "".equals(pageNo)){
		pn=0;
	}else{
		pn = Integer.parseInt(pageNo);
	}
	//每页多少条
	int pageSize = 10;
	int uid = (Integer)session.getAttribute("id");
	String type = request.getParameter("type");
	String beginDate = request.getParameter("beginDate");
	String endDate = request.getParameter("endDate");
	String searchType = request.getParameter("searchType");
	String searchWord = request.getParameter("searchWord");
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	List ob = new ArrayList();
	Map map = null;
	
	ob.add(new Object[]{"eq","reqUserId",uid});
	
	if(beginDate != null &&!"".equals(beginDate)){
		Date date = sdf.parse(beginDate);
		ob.add(new Object[]{"ge","reqDate",date});
	}
	
	if(endDate != null &&!"".equals(endDate)){
		Date date = sdf.parse(endDate);
		ob.add(new Object[]{"le","reqDate",date});
	}

	if("1".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","reqCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","p.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","p.name",searchWord});
		}
		
		map = purreqDAO.getPurreq(pn,pageSize,
				new String[][]{new String[]{"desc","date"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","reqCode"},new String[]{"","reqDate"},new String[]{"","p.code"},new String[]{"","p.name"},new String[]{"","state"}},
				new String[]{"project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		request.setAttribute("pages",pageList);
	}else if("2".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","outCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","p.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","p.name",searchWord});
		}
		
		map = outreqDAO.getOutreq(pn,pageSize,
				new String[][]{new String[]{"desc","id"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","outCode"},new String[]{"","reqDate"},new String[]{"","p.code"},new String[]{"","p.name"},new String[]{"","phase"}},
				new String[]{"","project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		request.setAttribute("pages",pageList);
	}else if("3".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","reqCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","op.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","op.name",searchWord});
		}
		
		map = retreqDAO.getRetreq(pn,pageSize,
				new String[][]{new String[]{"desc","reqDate"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","reqCode"},new String[]{"","reqDate"},new String[]{"","op.code"},new String[]{"","op.name"},new String[]{"","phase"}},
				new String[]{"outreq","project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		request.setAttribute("pages",pageList);
	}else if("4".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","borCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","p.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","p.name",searchWord});
		}
		
		map = borreqDAO.getBorreq(pn,pageSize,
				new String[][]{new String[]{"desc","id"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","borCode"},new String[]{"","reqDate"},new String[]{"","p.code"},new String[]{"","p.name"},new String[]{"","phase"}},
				new String[]{"","project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		request.setAttribute("pages",pageList);
	}else if("5".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","toCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","op.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","op.name",searchWord});
		}
		
		map = toreqDAO.getTomatreq(pn,pageSize,
				new String[][]{new String[]{"desc","id"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","toCode"},new String[]{"","reqDate"},new String[]{"","op.code"},new String[]{"","op.name"},new String[]{"","phase"}},
				new String[]{"borreq","project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		request.setAttribute("pages",pageList);
	}else if("6".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","backCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","op.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","op.name",searchWord});
		}
		
		map = backreqDAO.getBackreq(pn,pageSize,
				new String[][]{new String[]{"desc","id"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","backCode"},new String[]{"","reqDate"},new String[]{"","op.code"},new String[]{"","op.name"},new String[]{"","phase"}},
				new String[]{"borreq","project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		request.setAttribute("pages",pageList);
	}

	request.setAttribute("searchType",searchType);
	request.setAttribute("searchWord",searchWord);
	request.setAttribute("type",type);
	pageContext.forward("/vm/search/seachRequest.vm");
%>